package com.niuke;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;

/**
 * 20. 找单词
 * https://blog.csdn.net/m0_46181452/article/details/131667923
 */
public class NiukeMoni20 {

    public static int[][] offsets = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int n = Integer.parseInt(scanner.nextLine());
        char[][] arr = new char[n][n];
        for (int i = 0; i < n; i++) {
            String[] row = scanner.nextLine().split(",");
            char[] item = new char[n];
            for (int j = 0; j < row.length; j++) {
                item[j] = row[j].charAt(0);
            }
            arr[i] = item;
        }

        String input = scanner.nextLine();
        char[] inputArr = input.toCharArray();


        // 数据输入完毕后的处理
        List<List<Loc>> list = new ArrayList<>();
        for (int i = 0; i < inputArr.length; i++) {
            char curr = inputArr[i];
            System.out.println(i + " --- curr = " + curr);
            if (list.size() == 0) { // 第一个字母匹配，或者匹配到某个字母时所有情况都断掉了
                if (i != 0) { // 所有情况都断掉的
                    break;
                }
                for (int j = 0; j < n; j++) {
                    for (int k = 0; k < n; k++) {
                        if (arr[j][k] == curr) {
                            ArrayList<Loc> item = new ArrayList<>();
                            Loc loc = new Loc(j, k);
                            System.out.println(arr[j][k] + ", " + loc);
                            item.add(loc);
                            list.add(item);
                        }
                    }
                }
                if (list.size() == 0) { // 第一个字母就匹配不到
                    break;
                }
            } else {
                List<List<Loc>> newAdd = new ArrayList<>();
                for (int j = 0; j < list.size(); j++) {
                    List<Loc> item = list.get(j);
                    Loc lastLoc = item.get(item.size() - 1);
                    List<Loc> next = new ArrayList<>();
                    int tempX = lastLoc.x;
                    int tempY = lastLoc.y;
                    for (int[] offset : offsets) {
                        int xx = tempX + offset[0];
                        int yy = tempY + offset[1];
                        if (xx >= 0 && xx < n && yy >= 0 && yy < n && arr[xx][yy] == curr) {
                            Loc newL = new Loc(xx, yy);
                            if (!hasLoc(item, newL)) {
                                next.add(newL);
                            }
                        }
                    }

                    for (int k = 0; k < next.size(); k++) {
                        List<Loc> newItem = new ArrayList<>(item);
                        newItem.add(next.get(k));
                        newAdd.add(newItem);
                    }

                }

                list.clear();
                list.addAll(newAdd);
            }
        }

        if (list.size() == 0) {
            System.out.println("N");
        } else {
            for (int i = 0; i < list.size(); i++) {
                List<Loc> result = list.get(i);
                printLocList(result.size(), result);
            }
        }

    }

    public static boolean hasLoc(List<Loc> item, Loc newL) {
        for (int i = 0; i < item.size(); i++) {
            Loc l = item.get(i);
            if (l.x == newL.x && l.y == newL.y) {
                return true;
            }
        }
        return false;
    }

    private static void printLocList(int n, List<Loc> result) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < result.size(); j++) {
            Loc l = result.get(j);
            sb.append(l.x);
            sb.append(",");
            sb.append(l.y);
            if (j != n - 1) {
                sb.append(",");
            }
        }
        System.out.println(sb.toString());
    }

    public static class Loc {
        public int x;
        public int y;

        public Loc(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @NonNull
        @Override
        public String toString() {
            return "Loc (" + x + ", " + y + ")";
        }
    }

}
